class WordCommentsController < ApplicationController
  layout "words"
  before_filter(:get_word)


  # GET /word_comments/new
  # GET /word_comments/new.xml
  def new
    @word_comment = WordComment.new
  #  logger.info "Word Comment New Created"
  #  logger.info "word_id is " + params[:word_id].to_s
  #  logger.info "word_id is " + @word_id.to_s
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @word_comment }
    end
  end

  # GET /word_comments/1/edit
  def edit
    @word_comment = WordComment.find(params[:id])
  end

  # POST /word_comments
  # POST /word_comments.xml
  def create
    @word_comment = WordComment.new(params[:word_comment])
    @word_comment.commentor_id = session[:user]
    @word_comment.word_id = @word.id
    respond_to do |format|
      if @word_comment.save
        flash[:notice] = 'WordComment was successfully created.'
        format.html { redirect_to(@word) }
        format.xml  { render :xml => @word_comment, :status => :created, :location => @word_comment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @word_comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /word_comments/1
  # PUT /word_comments/1.xml
  def update
    @word_comment = WordComment.find(params[:id])

    respond_to do |format|
      if @word_comment.update_attributes(params[:word_comment])
        flash[:notice] = 'WordComment was successfully updated.'
        format.html { redirect_to(@word_comment) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @word_comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /word_comments/1
  # DELETE /word_comments/1.xml
  def destroy
    @word_comment = WordComment.find(params[:id])
    @word_comment.destroy

    respond_to do |format|
      format.html { redirect_to(word_comments_url) }
      format.xml  { head :ok }
    end
  end
  
  def mark_good
    @word_comment = WordComment.find(params[:id])
    @word_comment.rating += 1
    @word_comment.save!
    render :update do |page|
      page.replace_html "comment#{params[:id]}", :partial => "comment", :object => @word_comment
    end

  end
  
  def mark_bad
    @word_comment = WordComment.find(params[:id])
    @word_comment.rating -= 1
    @word_comment.save!

    @users_comment = UsersComment.find_by_user_id_and_comment_id(session[:user], @word_comment.id )
    if @user_comment.blank?
      #logger.info "Found or not? Found!"
      @users_comment = UsersComment.new
      @users_comment.user_id = session[:user]
      @users_comment.comment_id = @word_comment.id
    end    
    #logger.info "Everything Happened Here?"
    @users_comment.visible = "false"
    #logger.info "Everything Happened Here? No"
    @users_comment.save!    
    # we might need to redirect to somewhere else
    render :update do |page|
      page.hide "comment#{params[:id]}"
    end
  end
  
  
  private
    def get_word
      @word = Word.find(params[:word_id])
    end
end
